package org.ncibi.cytoscape.metscape.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;

import org.ncibi.cytoscape.metscape.utils.NumUtils;

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVWriter;

public class TextFile implements DataFile {

	private Vector<Vector<String>> data;
	
	public TextFile() {
		data = new Vector<Vector<String>>();
	}
	
	public TextFile(File file) throws IOException {
		open(file);
	}

	public void open(File file) throws IOException{
		BufferedReader input = new BufferedReader(new FileReader(file));
		String line=input.readLine();
		CSVParser commaParser = new CSVParser();
		CSVParser tabParser = new CSVParser('\t');
		CSVParser parser;
		if(commaParser.parseLine(line).length > 1) {
			parser = commaParser;
		}
		else {
			parser = tabParser;
		}
		data = new Vector<Vector<String>>();
		while(line != null) {
			String[] rowArray = parser.parseLine(line);
			Vector<String> row = new Vector<String>(Arrays.asList(rowArray));
			data.add(row);
			line = input.readLine();
		}
	    input.close();
	}
	
	public void save(File file) throws IOException {
		CSVWriter output = new CSVWriter(new FileWriter(file));
		for(Vector<String> row : data) {
			output.writeNext((String[]) row.toArray(new String[0]));
		}
		output.close();
	}
	
	public void setValue(Double value, int row, int col) {
		if(data.size() < (row+1))
			data.setSize(row+1);
			
		if(data.get(row) == null)
			data.set(row,new Vector<String>());
		
		if(data.get(row).size() < (col+1))
			data.get(row).setSize(col+1);
		
		if(value != null)
			data.get(row).set(col, value.toString());
		else
			data.get(row).set(col, null);
			
   	}
	
	public void setValue(Integer value, int row, int col) {
		if(data.size() < (row+1))
			data.setSize(row+1);

		if(data.get(row) == null)
			data.set(row,new Vector<String>());
		
		if(data.get(row).size() < (col+1))
			data.get(row).setSize(col+1);
		
		if(value != null)
			data.get(row).set(col, value.toString());
		else
			data.get(row).set(col, null);
   	}
	
	public void setValue(String value, int row, int col) {
		if(data.size() < (row+1))
			data.setSize(row+1);
		
		if(data.get(row) == null)
			data.set(row,new Vector<String>());
		
		if(data.get(row).size() < (col+1))
			data.get(row).setSize(col+1);
		
		data.get(row).set(col, value);
   	}
	
	public String getString(int row, int col){
		if(data.size() <= row || data.get(row) == null || data.get(row).size() <= col)
			return null;
		else
			return data.get(row).get(col);
	}

   	public Integer getInteger(int row, int col){
   		if(data.size() <= row || data.get(row) == null || data.get(row).size() <= col)
			return null;
		else
			return NumUtils.toInteger(data.get(row).get(col));
   	}
   	
   	public Double getDouble(int row, int col){
   		if(data.size() <= row || data.get(row) == null || data.get(row).size() <= col)
			return null;
		else
			return NumUtils.toDouble(data.get(row).get(col));
   	}
   	
	public int getStartRowIndex() {
		if(data.size() == 0)
			return -1;
		else return 0;
	}
	
	public int getEndRowIndex() {
		return (data.size() - 1);
	}
	
	public int getStartColIndex(int row) {
		if(data.size() <= row || data.get(row) == null)
			return -1;
		else
			return 0;
	}

	public int getEndColIndex(int row) {
		if(data.size() <= row || data.get(row) == null)
			return -1;
		else
			return(data.get(row).size() - 1);
	}
   	
}
